package com.ruoyi.supplier.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.supplier.domain.ProductSpec;
import com.ruoyi.supplier.domain.bo.ProductAlbumBo;
import com.ruoyi.supplier.domain.bo.ProductBo;
import com.ruoyi.supplier.domain.bo.ProductSkuBo;
import com.ruoyi.supplier.domain.bo.ProductSpecValueBo;
import com.ruoyi.supplier.domain.vo.*;
import com.ruoyi.supplier.domain.vo.product.*;
import com.ruoyi.supplier.domain.vo.spec.SpecPageQuery;
import com.ruoyi.supplier.manager.ProductManager;
import com.ruoyi.supplier.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品记录控制器
 * 前端访问路由地址为:/supplier/product
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    private final IProductService iProductService;

    @Autowired
    private ProductManager productManager;

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private IProductAlbumService productAlbumService;

    @Autowired
    private IProductSpecService productSpecService;

    @Autowired
    private IProductSpecValueService productSpecValueService;

    /**
     * 查询商品记录列表
     */
    @SaCheckPermission("supplier:product:list")
    @GetMapping("/list")
    public TableDataInfo<ProductVo> list(ProductBo bo, PageQuery pageQuery) {
        return iProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品记录列表
     */
    @SaCheckPermission("supplier:product:export")
    @Log(title = "商品记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductBo bo, HttpServletResponse response) {
        List<ProductVo> list = iProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品记录", ProductVo.class, response);
    }

    /**
     * 获取商品记录详细信息
     *
     * @param productId 主键
     */
    @SaCheckPermission("supplier:product:query")
    @GetMapping("/{productId}")
    public R<ProductVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer productId) {
        return R.ok(iProductService.queryById(productId));
    }

    /**
     * 新增商品记录
     */
    @SaCheckPermission("supplier:product:add")
    @Log(title = "商品记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductBo bo) {
        return toAjax(iProductService.insertByBo(bo));
    }

    /**
     * 修改商品记录
     */
    @SaCheckPermission("supplier:product:edit")
    @Log(title = "商品记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductBo bo) {
        return toAjax(iProductService.updateByBo(bo));
    }

    /**
     * 删除商品记录
     *
     * @param productIds 主键串
     */
    @SaCheckPermission("supplier:product:remove")
    @Log(title = "商品记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] productIds) {
        return toAjax(iProductService.deleteWithValidByIds(Arrays.asList(productIds), true));
    }



    /**
     * 分页查询
     */
    @PostMapping("/index")
    public R<ProductPageRespVo> index(@Validated @RequestBody ProductPageVo pageQuery) {
        ProductBo bo = new ProductBo();
        bo.setProductName(pageQuery.getKeyword());
        bo.setSupplierId(pageQuery.getSupplierId());
        TableDataInfo<ProductVo> dataInfo = iProductService.queryPageList(bo, pageQuery);
        ProductPageRespVo respVo = productManager.queryProduct(dataInfo);
        return R.ok(respVo,dataInfo.getTotal());
    }


    /**
     * 获取商品记录详细信息
     *
     * @param productId 主键
     */
    @GetMapping("/detail")
    public R<ProductDetailVo> detail(@NotNull(message = "主键不能为空") Integer productId) {
        ProductDetailVo detailVo = productManager.queryProductDetail(productId);
        return R.ok(detailVo);
    }

    /**
     * 新增商品记录
     */
    @PostMapping("/create")
    public R<Void> create(@Validated @RequestBody ProductSaveVo saveVo) {
        return toAjax(productManager.insertBySaveVo(saveVo));
    }

    /**
     * 新增商品记录
     */
    @PostMapping("/update")
    public R<Void> update(@Validated @RequestBody ProductSaveVo saveVo) {
        return toAjax(productManager.updateBySaveVo(saveVo));
    }

    /**
     * 获取商品记录详细信息
     *
     * @param productId 主键
     */
    @GetMapping("/delete")
    public R<Void> delete(@NotNull(message = "主键不能为空") Integer productId) {
        productManager.deleteById(productId);
        return R.ok();
    }

    /**
     * 商品上架
     *
     * @param productSkuId 主键
     */
    @GetMapping("/up")
    public R<Void> up(@NotNull(message = "主键不能为空") Integer productSkuId) {
        productManager.updateProductSkuStatus(productSkuId,1);
        return R.ok();
    }

    /**
     * 商品下家
     *
     * @param productSkuId 主键
     */
    @GetMapping("/down")
    public R<Void> down(@NotNull(message = "主键不能为空") Integer productSkuId) {
        productManager.updateProductSkuStatus(productSkuId,0);
        return R.ok();
    }


    /**
     * 新增商品记录
     */
    @PostMapping("/specList")
    public TableDataInfo<ProductSpecVo> specList(@Validated @RequestBody SpecPageQuery pageQuery) {
        return productManager.specList(pageQuery);
    }

    /**
     * 新增商品记录
     */
    @PostMapping("/addSpec")
    public R<Void> addSpec(String specName,String specValue,Integer supplierId) {
        ProductSpecVo specVo = new ProductSpecVo();
        if(StrUtil.isNotBlank(specValue)){
            ProductSpecValueVo valueVo = new ProductSpecValueVo();
            valueVo.setSpecValue(specValue);
            specVo.setItems(Lists.newArrayList(valueVo));
        }
        specVo.setSpecName(specName);
        specVo.setSupplierId(supplierId);
        String key = productManager.addSpec(specVo);
        return R.ok(key);
    }


    /**
     * 删除
     *
     * @param specId 主键
     */
    @PostMapping("/delSpec")
    public R<Void> delSpec(@NotNull(message = "主键不能为空") Integer specId) {
        String key = productManager.delSpec(specId);
        return R.ok(key);
    }

    /**
     * 添加规格值
     */
    @PostMapping("/addSpecValue")
    public R<Void> addSpecValue(String specId,String specValue) {
        ProductSpecValueVo valueVo = new ProductSpecValueVo();
        valueVo.setSpecId(Long.valueOf(specId));
        valueVo.setSpecValue(specValue);
        String key = productManager.addSpecValue(valueVo);
        return R.ok(key);
    }

    /**
     * 删除
     *
     * @param specValueId 主键
     */
    @PostMapping("/delSpecValue")
    public R<Void> delSpecValue(@NotNull(message = "主键不能为空") Integer specValueId) {
        String key = productManager.delSpecValue(specValueId);
        return R.ok(key);
    }

}
